package logs

import (
	"gitee.com/liuqiaosz/gokits/utils"
)

type logFactory struct {
	log     *proxy
	options *LogOptions
	creator LoggerCreator

}

func (f *logFactory) SetLogCreator(creator LoggerCreator, options *LogOptions) error {
	f.creator = creator
	log, err := creator.Create(options)
	if err != nil {
		return err
	}
	f.log = &proxy{
		logger:  log,
		options: options,
	}
	if nil != options.hook {
		f.log.AddHook(options.hook)
	}
	return nil
}

func (f *logFactory) SetDefaultOptions(options *LogOptions) error {
	return f.SetLogCreator(&zapLogCreator{}, options)
}

func (f *logFactory) getLogger() Logger {
	if f.log == nil {
		creator := &zapLogCreator{}
		f.SetLogCreator(creator, &LogOptions{
			fileName:     utils.GetLogNameByAppName(),
			fileSize:     10,
			maxFileCount: 30,
			debug:        false,
			compress:     true,
			json:         false,
			handler:      nil,
		})
	}
	return f.log
}

var factory *logFactory

func GetFactory() *logFactory {
	if nil == factory {
		factory = &logFactory{}
	}
	return factory
}

func Get() Logger {
	return GetFactory().getLogger()
}
